﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace ReactiveCode.Expressions
{
    [System.Diagnostics.DebuggerTypeProxy(typeof(ProxyExpression.DebugView))]
    class ProxyExpression: ReactiveExpression
    {
        #region DebugView

        class DebugView
        {
            [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
            public ReactiveExpression Real;
            public DebugView(ProxyExpression expr)
            {
                Real = expr.Real;
            }
        }

        #endregion

        IDisposable _enabledSubscription;

        ReactiveExpression _real;
        public ReactiveExpression Real
        {
            get { return _real; }
            set
            {
                if (_real == value)
                    return;

                if (_enabledSubscription != null)
                {
                    _enabledSubscription.Dispose();
                    _enabledSubscription = null;
                }

                _real = value;

                if (_real != null)
                    _enabledSubscription = BindEnabled(_real);
            }
        }

        public ProxyExpression(ReactiveExpression real = null)
        {
            Real = real;
        }

        public override string ToString()
        {
            return Real.ToString();
        }
    }
}
